package com.dykj.fireFighting.web.mq;

import com.alibaba.fastjson.JSON;

import com.dykj.fireFighting.api.elasticsearch.devHisGas.entity.EsDevHisGas;
import com.dykj.fireFighting.api.elasticsearch.devHisGas.service.EsDevHisGasService;
import com.dykj.fireFighting.common.starter.redisson.service.RedisService;

import com.dykj.fireFighting.web.entity.HaiManGas;
import com.dykj.fireFighting.web.message.InsertDevHisGasMessage;

import com.dykj.fireFighting.web.service.DevHisGasService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zhengbo
 */
@Service
@RocketMQMessageListener(
        topic = InsertDevHisGasMessage.TOPIC,
        consumerGroup = "dy-consumer-group-" + InsertDevHisGasMessage.TOPIC
)
public class InsertDevHisGasConsumer implements RocketMQListener<InsertDevHisGasMessage> {
    public static final Logger LOGGER = LoggerFactory.getLogger(InsertDevHisGasConsumer.class);
    @Autowired
    private RedisService redisService;
    @DubboReference
    private EsDevHisGasService esDevHisGasService;
    @Autowired
    private DevHisGasService devHisGasService;
    @Override
    public void onMessage(InsertDevHisGasMessage insertDevHisGasMessage) {
        HaiManGas haiManGas = insertDevHisGasMessage.getHaiManGas();
        LOGGER.info("===========插入EsDevHisWater数据到mysql==============");
        devHisGasService.insertDevHisGas(haiManGas);
        LOGGER.info("===========插入EsDevHisWater数据到ES==============");
        EsDevHisGas esDevHisGas = new EsDevHisGas();
        BeanUtils.copyProperties(haiManGas, esDevHisGas);
        System.out.println("===="+esDevHisGas.getId());
        esDevHisGasService.importAll(esDevHisGas);
        LOGGER.info("===========插入EsDevHisWater数据到ES:=============" + JSON.toJSONString(esDevHisGas));
        redisService.set(haiManGas.getId(), JSON.toJSONString(haiManGas), 3600L);
    }
}
